home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 90 / CDMM_90_1.ISO / Cycling Manager 2 / CyclingManager2Demo.exe / Disk1 / data1.cab / Game / DataCM2 / scripts / elements / animation.cnh next >
Encoding:
Text File  |  2002-05-10  |  13.3 KB  |  592 lines

  1. // animation.cnh
  2.  
  3. // message
  4. message StartAnim();
  5. message StopAnim();
  6. message PauseAnim();
  7. message AnimEnded(Gui_Component _pthis);
  8.  
  9. // Mutator
  10. func void Add_TranslateAnim(Gui_Component _poComponent,i32x _iPosX,i32x _iPosY,f32x _fSpeed);
  11. func void Add_TranslateScaleAnim(Gui_Component _poComponent,i32x _iPosX,i32x _iPosY,f32x _fScale,f32x _fSpeed);
  12. func void Add_FonduAnim(Gui_Component _poComponent,i32x _iEndColor,f32x _fSpeed);
  13. func void Add_Viewer3D(Gui_Component _poComponent,f32x _fMinRx,f32x _fMaxRx,f32x _fMinRy,f32x _fMaxRy,f32x _fSpeeRx,f32x _fSpeedRy);
  14.  
  15. // -------------------------------------------------------------
  16. // Standard animation interface
  17. // -------------------------------------------------------------
  18. var i32x ANIM_STOPPED = 0;
  19. var i32x ANIM_RUNNING = 1;
  20. var i32x ANIM_PAUSED = 2;
  21. var i32x ANIM_ENDED = 3;
  22.  
  23. func void FonduAnim_Start();
  24. func void FonduAnim_Stop();
  25. func void FonduAnim_Pause();
  26.  
  27. func void TranslateAnim_Start();
  28. func void TranslateAnim_Stop();
  29. func void TranslateAnim_Pause();
  30.  
  31. func void TranslateScaleAnim_Start();
  32. func void TranslateScaleAnim_Stop();
  33. func void TranslateScaleAnim_Pause();
  34.  
  35. func i32x Viewer3D_OnLoop(f32x _fDeltaTime);
  36. func i32x Viewer3D_OnMouseMove(i32x _iX,i32x _iY);
  37. // Data class
  38. class Gui_dtAnimTranslate
  39. {
  40.     var i32x iStartPosX;
  41.     var i32x iStartPosY;
  42.     var i32x iFinalPosX;
  43.     var i32x iFinalPosY;
  44.     var f32x fTheta;
  45.     var f32x fSpeed; // In rad per second
  46.     var i32x iState; // AnimState
  47. };
  48.  
  49. class Gui_dtAnimScaleTranslate
  50. {
  51.     var f32x fStartScale;
  52.     var f32x fFinalScale;
  53.     var i32x iStartPosX;
  54.     var i32x iStartPosY;
  55.     var i32x iFinalPosX;
  56.     var i32x iFinalPosY;
  57.     var f32x fTheta;
  58.     var f32x fSpeed; // In rad per second
  59.     var i32x iState; // AnimState
  60. };
  61. class Gui_dtAnimFondu
  62. {
  63.     var i32x iRs;
  64.     var i32x iGs;
  65.     var i32x iBs;
  66.     var i32x iAs;
  67.     var i32x iRe;
  68.     var i32x iGe;
  69.     var i32x iBe;
  70.     var i32x iAe;
  71.     var f32x fTheta;
  72.     var f32x fSpeed; // In rad per second
  73.     var i32x iState; // AnimState
  74. };
  75.  
  76. class Gui_dtViewer3D
  77. {
  78.     var f32x fMinRx;
  79.     var f32x fMinRy;
  80.     var f32x fMaxRx;
  81.     var f32x fMaxRy;
  82.     var f32x fRx;
  83.     var f32x fRy;
  84.     var i32x iOx;
  85.     var i32x iOy;
  86.     var f32x fSpeedRx;
  87.     var f32x fSpeedRy;
  88. };
  89.  
  90. func i32x TranslateLoop(f32x _fDelta);
  91. func i32x TranslateScaleLoop(f32x _fDelta);
  92. func i32x FonduLoop(f32x _fDelta);
  93.  
  94. interface Gui_iAnim_Translate
  95. {
  96.     // Standard anim message
  97.     TranslateAnim_Start StartAnim;
  98.     TranslateAnim_Stop StopAnim;
  99.     TranslateAnim_Pause PauseAnim;
  100.  
  101.     // Loop implementation
  102.     TranslateLoop Loop;
  103. }
  104.  
  105. interface Gui_iAnim_TranslateScale
  106. {
  107.     // Standard anim message
  108.     TranslateScaleAnim_Start StartAnim;
  109.     TranslateScaleAnim_Stop StopAnim;
  110.     TranslateScaleAnim_Pause PauseAnim;
  111.  
  112.     // Loop implementation
  113.     TranslateScaleLoop Loop;
  114. }
  115.  
  116. interface Gui_iAnim_Fondu
  117. {
  118.     // Standard anim message
  119.     FonduAnim_Start StartAnim;
  120.     FonduAnim_Stop StopAnim;
  121.     FonduAnim_Pause PauseAnim;
  122.  
  123.     // Loop implementation
  124.     FonduLoop Loop;
  125. }
  126.  
  127. interface Gui_iViewer3D
  128. {
  129.     Viewer3D_OnMouseMove MouseMove;
  130.     Viewer3D_OnLoop Loop;
  131. }
  132.  
  133. func void Add_FonduAnim(Gui_Component _poComponent,i32x _iEndColor,f32x _fSpeed)
  134. {
  135.     var Gui_dtAnimFondu pdtData;
  136.  
  137.     // New fonud anim data
  138.     pdtData = new Gui_dtAnimFondu;
  139.     pdtData.iRs = GetRed(GetModulateColor(_poComponent));
  140.     pdtData.iGs = GetGreen(GetModulateColor(_poComponent));
  141.     pdtData.iBs = GetBlue(GetModulateColor(_poComponent));
  142.     pdtData.iAs = GetAlpha(GetModulateColor(_poComponent));
  143.     pdtData.iRe = GetRed(_iEndColor);
  144.     pdtData.iGe = GetGreen(_iEndColor);
  145.     pdtData.iBe = GetBlue(_iEndColor);
  146.     pdtData.iAe = GetAlpha(_iEndColor);
  147.     pdtData.fTheta = 0.0;
  148.     pdtData.fSpeed = _fSpeed;
  149.     pdtData.iState = ANIM_STOPPED;
  150.  
  151.     // Add interface to component
  152.     AddInterface(_poComponent,Gui_iAnim_Fondu,pdtData);
  153. }
  154.  
  155. func void Add_TranslateAnim(Gui_Component _poComponent,i32x _iPosX,i32x _iPosY,f32x _fSpeed)
  156. {
  157.     var Gui_dtAnimTranslate pdtTrans;
  158.  
  159.     // New translation anim data
  160.     pdtTrans = new Gui_dtAnimTranslate;
  161.     pdtTrans.iStartPosX = RelPosX(_poComponent);
  162.     pdtTrans.iStartPosY = RelPosY(_poComponent);
  163.     pdtTrans.iFinalPosX = _iPosX;
  164.     pdtTrans.iFinalPosY = _iPosY;
  165.     pdtTrans.fTheta = 0.0;
  166.     pdtTrans.fSpeed = _fSpeed;
  167.     pdtTrans.iState = ANIM_STOPPED;
  168.  
  169.     // Add interface to component
  170.     AddInterface(_poComponent,Gui_iAnim_Translate,pdtTrans);
  171. }
  172. func void Add_TranslateScaleAnim(Gui_Component _poComponent,i32x _iPosX,i32x _iPosY,f32x _fScale,f32x _fSpeed)
  173. {
  174.     var Gui_dtAnimScaleTranslate pdtTrans;
  175.  
  176.     // New translation anim data
  177.     pdtTrans = new Gui_dtAnimScaleTranslate;
  178.     pdtTrans.iStartPosX = RelPosX(_poComponent);
  179.     pdtTrans.iStartPosY = RelPosY(_poComponent);
  180.     pdtTrans.iFinalPosX = _iPosX;
  181.     pdtTrans.iFinalPosY = _iPosY;
  182.     pdtTrans.fStartScale = GetScale(_poComponent);
  183.     pdtTrans.fFinalScale = _fScale;
  184.     pdtTrans.fTheta = 0.0;
  185.     pdtTrans.fSpeed = _fSpeed;
  186.     pdtTrans.iState = ANIM_STOPPED;
  187.  
  188.     // Add interface to component
  189.     AddInterface(_poComponent,Gui_iAnim_TranslateScale,pdtTrans);
  190. }
  191.  
  192. func void Add_Viewer3D(Gui_Component _poComponent,f32x _fMinRx,f32x _fMaxRx,f32x _fMinRy,f32x _fMaxRy,f32x _fSpeeRx,f32x _fSpeedRy)
  193. {
  194.     var Gui_dtViewer3D pdtData;
  195.  
  196.     // New viewer 3d data
  197.     pdtData = new Gui_dtViewer3D;
  198.     pdtData.fMinRx = _fMinRx;
  199.     pdtData.fMinRy = _fMinRy;
  200.     pdtData.fMaxRx = _fMaxRx;
  201.     pdtData.fMaxRy = _fMaxRy;
  202.     pdtData.fRx = 0.0;
  203.     pdtData.fRy = 0.0;
  204.     pdtData.iOx = 0;
  205.     pdtData.iOy = 0;
  206.     
  207.     pdtData.fSpeedRx = _fSpeeRx;
  208.     pdtData.fSpeedRy = _fSpeedRy;
  209.  
  210.     // Add interface to component
  211.     AddInterface(_poComponent,Gui_iViewer3D,pdtData);
  212. }
  213.  
  214. func void TranslateAnim_Start()
  215. {
  216.     var Gui_Component pthis;
  217.     var Gui_dtAnimTranslate pdtData;
  218.  
  219.     // Retrieve this pointer
  220.     pthis = GetThis();
  221.  
  222.     // Retrieve translation data
  223.     pdtData = GetData(pthis);
  224.  
  225.     // Change anim state
  226.     pdtData.iState = ANIM_RUNNING;
  227. }
  228.  
  229. func void TranslateAnim_Stop()
  230. {
  231.     var Gui_Component pthis;
  232.     var Gui_dtAnimTranslate pdtData;
  233.  
  234.     // Retrieve this pointer
  235.     pthis = GetThis();
  236.  
  237.     // Retrieve translation data
  238.     pdtData = GetData(pthis);
  239.  
  240.     // Change anim state
  241.     pdtData.iState = ANIM_STOPPED;
  242.     pdtData.fTheta = 0.0;
  243. }
  244.  
  245. func void TranslateAnim_Pause()
  246. {
  247.     var Gui_Component pthis;
  248.     var Gui_dtAnimTranslate pdtData;
  249.  
  250.     // Retrieve this pointer
  251.     pthis = GetThis();
  252.  
  253.     // Retrieve translation data
  254.     pdtData = GetData(pthis);
  255.  
  256.     // Change anim state
  257.     pdtData.iState = ANIM_PAUSED;
  258. }
  259.  
  260. func void TranslateScaleAnim_Start()
  261. {
  262.     var Gui_Component pthis;
  263.     var Gui_dtAnimScaleTranslate pdtData;
  264.  
  265.     // Retrieve this pointer
  266.     pthis = GetThis();
  267.  
  268.     // Retrieve translation data
  269.     pdtData = GetData(pthis);
  270.  
  271.     // Change anim state
  272.     pdtData.iState = ANIM_RUNNING;
  273. }
  274.  
  275. func void TranslateScaleAnim_Stop()
  276. {
  277.     var Gui_Component pthis;
  278.     var Gui_dtAnimScaleTranslate pdtData;
  279.  
  280.     // Retrieve this pointer
  281.     pthis = GetThis();
  282.  
  283.     // Retrieve translation data
  284.     pdtData = GetData(pthis);
  285.  
  286.     // Change anim state
  287.     pdtData.iState = ANIM_STOPPED;
  288.     pdtData.fTheta = 0.0;
  289. }
  290.  
  291. func void TranslateScaleAnim_Pause()
  292. {
  293.     var Gui_Component pthis;
  294.     var Gui_dtAnimScaleTranslate pdtData;
  295.  
  296.     // Retrieve this pointer
  297.     pthis = GetThis();
  298.  
  299.     // Retrieve translation data
  300.     pdtData = GetData(pthis);
  301.  
  302.     // Change anim state
  303.     pdtData.iState = ANIM_PAUSED;
  304. }
  305.  
  306. func void FonduAnim_Start()
  307. {
  308.     var Gui_Component pthis;
  309.     var Gui_dtAnimFondu pdtData;
  310.  
  311.     // Retrieve this pointer
  312.     pthis = GetThis();
  313.  
  314.     // Retrieve translation data
  315.     pdtData = GetData(pthis);
  316.  
  317.     // Change anim state
  318.     pdtData.iState = ANIM_RUNNING;
  319. }
  320.  
  321. func void FonduAnim_Stop()
  322. {
  323.     var Gui_Component pthis;
  324.     var Gui_dtAnimFondu pdtData;
  325.  
  326.     // Retrieve this pointer
  327.     pthis = GetThis();
  328.  
  329.     // Retrieve translation data
  330.     pdtData = GetData(pthis);
  331.  
  332.     // Change anim state
  333.     pdtData.iState = ANIM_STOPPED;
  334.     pdtData.fTheta = 0.0;
  335. }
  336.  
  337. func void FonduAnim_Pause()
  338. {
  339.     var Gui_Component pthis;
  340.     var Gui_dtAnimFondu pdtData;
  341.  
  342.     // Retrieve this pointer
  343.     pthis = GetThis();
  344.  
  345.     // Retrieve translation data
  346.     pdtData = GetData(pthis);
  347.  
  348.     // Change anim state
  349.     pdtData.iState = ANIM_PAUSED;
  350. }
  351.  
  352. func i32x FonduLoop(f32x _fDelta)
  353. {
  354.     var Gui_Component pthis,pparent;
  355.     var Gui_dtAnimFondu pdtData;
  356.     var f32x pcos;
  357.     var f32x fRs,fAs,fBs,fGs;
  358.     var f32x fRe,fAe,fBe,fGe;
  359.     var f32x fRc,fAc,fBc,fGc;
  360.  
  361.     // Retrieve this pointer
  362.     pthis = GetThis();
  363.  
  364.     // Retrieve translation data
  365.     pdtData = GetData(pthis);
  366.  
  367.     if(pdtData.iState == ANIM_RUNNING)
  368.     {
  369.         // Current Position
  370.         pdtData.fTheta = pdtData.fTheta + pdtData.fSpeed * _fDelta;
  371.         if(pdtData.fTheta>MATH_PI)
  372.             pdtData.fTheta = MATH_PI;
  373.  
  374.         pcos = 1.0 - ((cos(pdtData.fTheta)+1.0) * 0.5);
  375.  
  376.         fRs = pdtData.iRs;
  377.         fGs = pdtData.iGs;
  378.         fBs = pdtData.iBs;
  379.         fAs = pdtData.iAs;
  380.  
  381.  
  382.         fRe = pdtData.iRe;
  383.         fGe = pdtData.iGe;
  384.         fBe = pdtData.iBe;
  385.         fAe = pdtData.iAe;
  386.  
  387.         // Compute new modulate color
  388.         fRc = fRs + pcos * (fRe - fRs);
  389.         fGc = fGs + pcos * (fGe - fGs);
  390.         fBc = fBs + pcos * (fBe - fBs);
  391.         fAc = fAs + pcos * (fAe - fAs);
  392.  
  393.         if(pdtData.fTheta==MATH_PI)
  394.         {
  395.             // End Anim
  396.             pdtData.iState = ANIM_ENDED;
  397.         }
  398.  
  399.         // Update modulate color
  400.         SetModulateColor(pthis,MakeARGB(fAc,fRc,fGc,fBc));
  401.     }
  402.     else if(pdtData.iState == ANIM_ENDED)
  403.     {
  404.         // Remove loop interface
  405.         RemoveInterface(pthis);
  406.         pparent = GetParent(pthis);
  407.         if(pparent)
  408.             pparent<<AnimEnded(pthis);
  409.     }
  410. }
  411. func i32x TranslateLoop(f32x _fDelta)
  412. {
  413.     var Gui_Component pthis,pparent;
  414.     var Gui_dtAnimTranslate pdtData;
  415.     var f32x fSX,fSY,fFX,fFY,pcos,fPosX,fPosY;
  416.  
  417.     // Retrieve this pointer
  418.     pthis = GetThis();
  419.  
  420.     // Retrieve translation data
  421.     pdtData = GetData(pthis);
  422.  
  423.     if(pdtData.iState == ANIM_RUNNING)
  424.     {
  425.         // Current Position
  426.         pdtData.fTheta = pdtData.fTheta + pdtData.fSpeed * _fDelta;
  427.         if(pdtData.fTheta>MATH_PI)
  428.             pdtData.fTheta = MATH_PI;
  429.  
  430.         pcos = 1.0 - ((cos(pdtData.fTheta)+1.0) * 0.5);
  431.  
  432.         fSX = pdtData.iStartPosX;
  433.         fSY = pdtData.iStartPosY;
  434.  
  435.         fFX = pdtData.iFinalPosX;
  436.         fFY = pdtData.iFinalPosY;
  437.  
  438.         // Compute new position
  439.         fPosX = fSX + pcos * (fFX - fSX);
  440.         fPosY = fSY + pcos * (fFY - fSY);
  441.  
  442.         if(pdtData.fTheta==MATH_PI)
  443.         {
  444.             // End Anim
  445.             pdtData.iState = ANIM_ENDED;
  446.         }
  447.  
  448.         // Move component
  449.         MoveTo(pthis,fPosX,fPosY);
  450.     }
  451.     else if(pdtData.iState == ANIM_ENDED)
  452.     {
  453.         pparent = GetParent(pthis);
  454.         if(pparent)
  455.             pparent<<AnimEnded(pthis);
  456.         // Remove loop interface
  457.         RemoveInterface(pthis);
  458.     }
  459. }
  460.  
  461. func i32x TranslateScaleLoop(f32x _fDelta)
  462. {
  463.     var Gui_Component pthis,pparent;
  464.     var Gui_dtAnimScaleTranslate pdtData;
  465.     var f32x fSScale,fFScale,fScale,fSX,fSY,fFX,fFY,pcos,fPosX,fPosY;
  466.  
  467.     // Retrieve this pointer
  468.     pthis = GetThis();
  469.  
  470.     // Retrieve translation data
  471.     pdtData = GetData(pthis);
  472.  
  473.     if(pdtData.iState == ANIM_RUNNING)
  474.     {
  475.         // Current Position
  476.         pdtData.fTheta = pdtData.fTheta + pdtData.fSpeed * _fDelta;
  477.         if(pdtData.fTheta>MATH_PI)
  478.             pdtData.fTheta = MATH_PI;
  479.  
  480.         pcos = 1.0 - ((cos(pdtData.fTheta)+1.0) * 0.5);
  481.  
  482.         fSScale = pdtData.fStartScale;
  483.         fSX = pdtData.iStartPosX;
  484.         fSY = pdtData.iStartPosY;
  485.  
  486.         fFScale = pdtData.fFinalScale;
  487.         fFX = pdtData.iFinalPosX;
  488.         fFY = pdtData.iFinalPosY;
  489.  
  490.         // Compute new position
  491.         fPosX = fSX + pcos * (fFX - fSX);
  492.         fPosY = fSY + pcos * (fFY - fSY);
  493.         fScale = fSScale + pcos * (fFScale - fSScale);
  494.  
  495.         if(pdtData.fTheta==MATH_PI)
  496.         {
  497.             // End Anim
  498.             pdtData.iState = ANIM_ENDED;
  499.         }
  500.  
  501.         // Move component
  502.         MoveTo(pthis,fPosX,fPosY);
  503.         SetScale(pthis,fScale);
  504.     }
  505.     else if(pdtData.iState == ANIM_ENDED)
  506.     {
  507.         pparent = GetParent(pthis);
  508.         if(pparent)
  509.             pparent<<AnimEnded(pthis);
  510.         // Remove loop interface
  511.         RemoveInterface(pthis);
  512.     }
  513. }
  514.  
  515. func i32x Viewer3D_OnMouseMove(i32x _iX,i32x _iY)
  516. {
  517.     var Gui_Component pthis,pparent;
  518.     var Gui_dtViewer3D pdtData;
  519.     var f32x dx,dy;
  520.  
  521.     // Retrieve this pointer
  522.     pthis = GetThis();
  523.  
  524.     // Retrieve translation data
  525.     pdtData = GetData(pthis);
  526.     if(GetKeyState(VK_LButton))
  527.     {
  528.         // Rotate
  529.         pdtData.fRx = pdtData.fRx + 0.018*(_iY - pdtData.iOy);
  530.         pdtData.fRy = pdtData.fRy + 0.018*(_iX - pdtData.iOx);
  531.         if(!((pdtData.fMaxRx == MATH_2PI)&&(pdtData.fMinRx == 0.0)))
  532.         {
  533.             if(pdtData.fRx>pdtData.fMaxRx)
  534.                 pdtData.fRx = pdtData.fMaxRx;
  535.             if(pdtData.fRx<pdtData.fMinRx)
  536.                 pdtData.fRx = pdtData.fMinRx;
  537.         }
  538.         if(!((pdtData.fMaxRy == MATH_2PI)&&(pdtData.fMinRy == 0.0)))
  539.         {
  540.             if(pdtData.fRy>pdtData.fMaxRy)
  541.                 pdtData.fRy = pdtData.fMaxRy;
  542.             if(pdtData.fRy<pdtData.fMinRy)
  543.                 pdtData.fRy = pdtData.fMinRy;
  544.         }
  545.         Rotate(GetSprite(pthis),pdtData.fRx,pdtData.fRy,0.0);
  546.     }
  547.     pdtData.iOx = _iX;
  548.     pdtData.iOy = _iY;
  549.     return 0;
  550. }
  551.  
  552. func i32x Viewer3D_OnLoop(f32x _fDeltaTime)
  553. {
  554.     var Gui_Component pthis,pparent;
  555.     var Gui_dtViewer3D pdtData;
  556.     var f32x dx,dy;
  557.     
  558.     // Retrieve this pointer
  559.     pthis = GetThis();
  560.     
  561.     // Retrieve translation data
  562.     pdtData = GetData(pthis);
  563.     // Rotate
  564.     if(pdtData.fSpeedRx!=0.0)
  565.     {
  566.         pdtData.fRx = pdtData.fRx + pdtData.fSpeedRx*_fDeltaTime;
  567.         if(!((pdtData.fMaxRx == MATH_2PI)&&(pdtData.fMinRx == 0.0)))
  568.         {
  569.             if(pdtData.fRx>pdtData.fMaxRx)
  570.                 pdtData.fRx = pdtData.fMaxRx;
  571.             if(pdtData.fRx<pdtData.fMinRx)
  572.                 pdtData.fRx = pdtData.fMinRx;
  573.         }
  574.     }
  575.     
  576.     if(pdtData.fSpeedRy!=0.0)
  577.     {
  578.         pdtData.fRy = pdtData.fRy + pdtData.fSpeedRy*_fDeltaTime;
  579.         if(!((pdtData.fMaxRy == MATH_2PI)&&(pdtData.fMinRy == 0.0)))
  580.         {
  581.             if(pdtData.fRy>pdtData.fMaxRy)
  582.                 pdtData.fRy = pdtData.fMaxRy;
  583.             if(pdtData.fRy<pdtData.fMinRy)
  584.                 pdtData.fRy = pdtData.fMinRy;
  585.         }
  586.     }
  587.  
  588.     Rotate(GetSprite(pthis),pdtData.fRx,pdtData.fRy,0.0);
  589.     
  590.     return 1;
  591. }
  592.